﻿
#include "menubar.h"
#include "mainwindow.h"
#include <QApplication>
MenuBar::MenuBar(QWidget* parent)
	: QMenuBar(parent)
{
	setStyleSheet(R"(
    /* 菜单栏整体：贴近 WPS 浅灰+细线风格 */
    QMenuBar {
        height: 42px; 
        background-color: #f7f7f7; /* WPS 同款浅灰背景 */
        border-bottom: 1px solid #dcdcdc; /* 更细腻的分割线 */
        font-family: "Microsoft YaHei", sans-serif; /* 适配中文的清晰字体 */
    }

    /* 一级菜单项： hover 渐变 + 图标间距优化 */
    QMenuBar::item {
        padding: 0 20px; /* 增大左右间距，贴近 WPS 宽松感 */
        margin: 0 2px;   /* 菜单项之间的微小间隔 */
        color: #333;     /* 深灰文字更清晰 */
        font-size: 13pt; /* 稍大字体，符合 WPS 阅读习惯 */
        border-radius: 4px; /* 圆角预备，hover 时更柔和 */
    }

    /* 鼠标悬停态：WPS 式渐变高亮 */
    QMenuBar::item:selected {
        background: qlineargradient(x1:0, y1:0, x2:0, y2:1, 
                                   stop:0 #e8f2ff, stop:1 #d9e8ff); /* 淡蓝渐变 */
        color: #0052cc; /* 强调色文字 */
    }

    /* 下拉菜单容器：宽度自适应 + 阴影 + 圆角 */
    QMenu {
        width: 220px; /* 比 WPS 稍窄，可根据需求调大 */
        font-size: 12pt; 
        background-color: #fff; 
        border: 1px solid #dcdcdc; /* 弱化边框 */
        border-radius: 6px; /* 柔和圆角 */
        box-shadow: 0 4px 12px rgba(0,0,0,0.08); /* 悬浮阴影更自然 */
        padding: 8px 0; /* 上下内边距，让菜单项不拥挤 */
    }

    /* 下拉菜单项：图标对齐 + hover 背景 */
    QMenu::item {
        height: 36px; 
        padding: 0 30px 0 40px; /* 增大左侧缩进，给图标留空间 */
        color: #333; 
        border-bottom: 1px solid #f2f2f2; /* 细微分割线 */
        /* 隐藏默认选中虚线框（更贴近 WPS 无焦点框设计） */
        selection-background-color: transparent; 
    }

    /* 下拉菜单项悬停：仿 WPS 淡灰 + 图标变色 */
    QMenu::item:selected {
        background-color: #f2f2f2; /* 浅灰高亮 */
        color: #0052cc; 
        /* 若菜单项有图标，可强制图标变色（需配合 QIcon 染色） */
        icon-color: #0052cc; 
    }

    /* 分割线优化：更细更淡 */
    QMenu::separator {
        height: 1px;
        background: #f2f2f2; 
        margin: 6px 0; /* 上下留白更透气 */
    }

    /* 快捷键文本：右对齐 + 浅灰配色（贴近 WPS 简洁感） */
    QMenu::item:shortcut {
        color: #999; 
        margin-left: auto; /* 快捷键右对齐 */
        padding-right: 15px;
    }
)");

	createOrGetMenu("file(F)");
	createOrGetMenu("edit(E)");
	createOrGetMenu("format(T)");
	createOrGetMenu("help(H)");
}

QAction* MenuBar::getAction(const QString& menuName, const QString& actionText)
{
	QString temp = actionText;
	return m_actions.value(temp.remove(" "), nullptr);
}

QMenu* MenuBar::createOrGetMenu(const QString& menuName)
{
	if (m_menus.contains(menuName))
		return m_menus[menuName];

	QMenu* menu = addMenu(menuName);  
	m_menus[menuName] = menu;
	if (menuName == "file(F)")
    {
		QAction* newAct = menu->addAction(tr("Create a new document"));
		m_actions["File_New"] = newAct;
		newAct->setIcon(QIcon(":/image/new.svg"));
		connect(newAct, &QAction::triggered, this, &MenuBar::newDocumentTriggered);

		QAction* openAct = menu->addAction(tr("open"));
		m_actions["File_Open"] = openAct;
		openAct->setIcon(QIcon(":/image/open.svg"));
		connect(openAct, &QAction::triggered, this, &MenuBar::openDocumentTriggered);

		QAction* saveAct = menu->addAction(tr("save"));
		m_actions["File_Save"] = saveAct;
		saveAct->setIcon(QIcon(":/image/save.svg"));
		connect(saveAct, &QAction::triggered, this, &MenuBar::saveDocumentTriggered);

		QAction* saveAsAct = menu->addAction(tr("saveAs"));
		m_actions["File_SaveAs"] = saveAsAct;
		saveAsAct->setIcon(QIcon(":/image/saveas.svg"));
		connect(saveAsAct, &QAction::triggered, this, &MenuBar::saveAsDocumentTriggered);

		QAction* exitAct = menu->addAction(tr("exit"));
		m_actions["File_Exit"] = exitAct;
		exitAct->setIcon(QIcon(":/image/exit.svg"));
		connect(exitAct, &QAction::triggered, this, &MenuBar::onExitTriggered);
		menu->setTitle(tr("file(F)"));
	}
    else if (menuName == "edit(E)")
    {
		QAction* undoAct = menu->addAction(tr("undo"));
		m_actions["Edit_Undo"] = undoAct;
		undoAct->setIcon(QIcon(":/image/undo.svg"));
		connect(undoAct, &QAction::triggered, this, [=]() {emit undo(); });
		

		QAction* redoAct = menu->addAction(tr("redo"));
		m_actions["Edit_Redo"] = redoAct;
		redoAct->setIcon(QIcon(":/image/redo.svg"));
		connect(redoAct, &QAction::triggered, this, [=]() {emit redo(); });

		QAction* copyAct = menu->addAction(tr("copy"));
		m_actions["Edit_Copy"] = copyAct;
		copyAct->setIcon(QIcon(":/image/copy.svg"));
		connect(copyAct, &QAction::triggered, this, [=]() {emit copy(); });

		QAction* pasteAct = menu->addAction(tr("paste"));
		m_actions["Edit_Paste"] = pasteAct;
		pasteAct->setIcon(QIcon(":/image/paste.svg"));
		connect(pasteAct, &QAction::triggered, this, [=]() {emit paste(); });
		
		QAction* cutAct = menu->addAction(tr("cut"));
		m_actions["Edit_Cut"] = cutAct;
		cutAct->setIcon(QIcon(":/image/cut.svg"));
		connect(cutAct, &QAction::triggered, this, [=]() {emit cut(); });
		menu->setTitle(tr("edit(E)"));
    }
	else if (menuName == "format(T)")
	{
		QAction* normalAct = menu->addAction(tr("normal"));
		m_actions["Format_Main"] = normalAct;
		normalAct->setIcon(QIcon(":/image/main.svg"));

		QAction* title1Act = menu->addAction(tr("title1"));
		m_actions["Format_Title1"] = title1Act;
		title1Act->setIcon(QIcon(":/image/title1.svg"));

		QAction* title2Act = menu->addAction(tr("title2"));
		m_actions["Format_Title2"] = title2Act;
		title2Act->setIcon(QIcon(":/image/title2.svg"));

		QAction* title3Act = menu->addAction(tr("title3"));
		m_actions["Format_Title3"] = title3Act;		
		title3Act->setIcon(QIcon(":/image/title3.svg"));

		QAction* boldAct = menu->addAction(tr("bold"));
		m_actions["Format_Emphasize"] = boldAct;
		boldAct->setIcon(QIcon(":/image/emphasize.svg"));

		QAction* codeAct = menu->addAction(tr("code"));
		m_actions["Format_Code"] = codeAct;
		codeAct->setIcon(QIcon(":/image/code.svg"));

		QAction* quoteAct = menu->addAction(tr("quote"));
		m_actions["Format_Quote"] = quoteAct;
		quoteAct->setIcon(QIcon(":/image/quote.svg"));

		QList<QAction*> styleActions = { normalAct, title1Act, title2Act, title3Act,boldAct, codeAct, quoteAct };
		foreach(QAction * act, styleActions)
		{
			QString stylename = "normal";
			if (act == normalAct)
				stylename = "正文";
			else if (act == title1Act)
				stylename = "标题1";
			else if (act == title2Act)
				stylename = "标题22";
			else if (act == title3Act)
				stylename = "标题3";
			else if (act == boldAct)
				stylename = "强调";
			else if (act == codeAct)
				stylename = "代码";
			else if (act == quoteAct)
				stylename = "引用";
			else 
				stylename = "正文";
			connect(act, &QAction::triggered, this, [=]() {emit fontStyleChange(stylename); });
		}
		menu->setTitle(tr("format(T)"));
	}
	else if (menuName == "help(H)")
	{
		QAction* aboutAct = menu->addAction(tr("about"));
		m_actions["Help_About"] = aboutAct;
		aboutAct->setIcon(QIcon(":/image/about.svg"));
		menu->setTitle(tr("help(H)"));
	}
	return menu;
}

void MenuBar::onExitTriggered()
{
	MainWindow* mainWindow = qobject_cast<MainWindow*>(parentWidget());
	bool needSave = false;

	QApplication::quit();
}
